package vg.modules.filter;


import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.util.Observable;

import javax.swing.ImageIcon;
import javax.swing.JMenuItem;
import javax.swing.KeyStroke;

import vg.services.graph_view_manager.interfaces.graph_view.AGraphView;
import vg.services.graph_view_manager.interfaces.tab.ATab;
import vg.services.plugin_manager.event_and_request.event.AUIEvent;
import vg.services.plugin_manager.event_and_request.event.UIEventChangeTab;
import vg.services.plugin_manager.interfaces.IPlugin;
import vg.services.plugin_manager.realization.PluginParameter;
import vg.services.user_interface_manager.interfaces.AMenuItem;

/**
 * This class connected filter's plugin.
 * @author tzolotuhin
 */
public class FilterPlugin implements IPlugin {
	private PluginParameter param = null;
	private JMenuItem jMenuItem;
	private AGraphView view;
	private FilterPanel filter;
	//-------------------------------------------------------------------------
	public void install(final PluginParameter parameter) throws Exception {
		this.param = parameter;
		this.filter = new FilterPanel("Filter", parameter);
		this.jMenuItem = new JMenuItem("Filter");
		this.jMenuItem.setIcon(new ImageIcon("./data/resources/textures/filter.png"));
		this.jMenuItem.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				filter.open();
				filter.changeView(view);
			}
		});
		this.jMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F, KeyEvent.CTRL_DOWN_MASK));
		this.param.userInterface.addMenuItem(new AMenuItem(jMenuItem) {
			public void update(Observable o, Object arg) {
				if(arg instanceof AUIEvent) {
					AUIEvent event = (AUIEvent)arg;
					switch(event.getType()) {
						case DEF_CHANGE_TAB:
						{
							UIEventChangeTab bufEvent = (UIEventChangeTab)event;
							ATab tab = bufEvent.getTab();
							if (tab != null) {
								switch (tab.getTabType()) {
									case ATab.DEF_GRAPH_VIEW_TAB_TYPE: {
										view = (AGraphView)tab;
										if(filter.isVisible()) {
											filter.changeView(view);
										}
										break;
									}							
								}
							}
							break;
						}
						case DEF_CLOSE_PROGRAM:
						{
							filter.dispose();
							break;
						}
						case DEF_CHANGE_UI_STYLE:
						{
							filter.updateUITheme();
							break;
						}
					}
				}
			}
		}, "edit");	
		this.filter.updateUITheme();
	}
}
